/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import JDBCUtil.MyConn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.admin.Castes;

/**
 *
 * @author Danh
 */
public class CasteManager {

    Connection conn;

    public CasteManager() {
        try {
            conn = MyConn.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Castes> getCates() {
        List<Castes> list = new ArrayList<Castes>();
        try {
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM Castes");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Castes c = new Castes(rs.getInt("Caste_Id"), rs.getNString("Caste_Name"), rs.getBoolean("Caste_Status"));
                list.add(c);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean insertCates(Castes c) {
        try {
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO Castes(Caste_Name,Caste_Status) VALUES(?,?)");
            pstmt.setNString(1, c.getCateName());
            pstmt.setBoolean(2, c.isCateStatus());
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
        }
        return false;
    }

    public boolean update(Castes c) {
        PreparedStatement pstmt;
        try {
            pstmt = conn.prepareStatement("UPDATE Castes SET Caste_Name=? ,Caste_Status =? WHERE Caste_Id=?");
            pstmt.setNString(1, c.getCateName());
            pstmt.setBoolean(2, c.isCateStatus());
            pstmt.setInt(3, c.getCateId());
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean deleteCate(int id) {
        try {
            PreparedStatement pstmt = conn.prepareStatement("DELETE FROM Castes  WHERE Caste_Id=?");
            pstmt.setInt(1, id);
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
        }

        return false;
    }
}
